package main

import (
	"fmt"
	"log"
	"os"
	"strconv"
	"time"
)

func OpenLog() *os.File{
	a,b,c := time.Now().Date()
	d := int(b)
	var e string
	switch d {
	case 1,2,3,4,5,6,7,8,9:
		e = "0"+strconv.Itoa(d)
	case 10,11,12:
		e = strconv.Itoa(d)
	}
	title := ".\\LOG\\"+strconv.Itoa(a) +e+strconv.Itoa(c)+".log"
	file,_ := os.OpenFile(title,os.O_CREATE|os.O_WRONLY|os.O_APPEND,0666)
	return file
}

func Info(mes string,err error)  {
	f := OpenLog()
	defer f.Close()
	log.SetOutput(f)
	log.SetPrefix("[INFO]:")
	log.SetFlags(log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
	log.Printf("%v%v\r\n",mes,err)
	fmt.Println("INFO",time.Now().Local(),":",mes,err)
}

func Warn(mes string,err error)  {
	f := OpenLog()
	defer f.Close()
	log.SetOutput(f)
	log.SetPrefix("[WARN]:")
	log.SetFlags(log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
	log.Printf("%v%v\r\n",mes,err)
	fmt.Println("WARN",time.Now().Local(),":",mes,err)
}

func Error(mes string,err error)  {
	f := OpenLog()
	defer f.Close()
	log.SetOutput(f)
	log.SetPrefix("[ERROR]:")
	log.SetFlags(log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
	log.Printf("%v%v\r\n",mes,err)
	fmt.Println("ERROR",time.Now().Local(),":",mes,err)
}

//func main() {
//	var err error
//	err = nil
//	Info("开始运行文件！",err)
//	time.Sleep(time.Second)
//	Warn("警告！出现偏差",err)
//	time.Sleep(time.Second)
//	Error("报警！出现错误！",err)
//	time.Sleep(time.Second)
//}
